#include "kdcdiscstructureplugin.h"

#include <KPluginFactory>
#include <KPluginLoader>

#include <KActionMenu>
#include <KAction>
#include <KActionCollection>
#include <KToolBar>
#include <KMenu>
#include <KVBox>

#include <kdclib.h>

class KDCDiscStructurePlugin;

#include <KLocale>
#include <QVBoxLayout>
#include <QHBoxLayout>

#include <kdcmplayerwindow.h>
#include <kdcdiscsettings.h>
#include <interfaces/kdcstatusbarinterface.h>
#include <interfaces/kdcjobsinterface.h>
KDCJobsInterface *gKDCJobsInterface;
#include <interfaces/kdcstatusbarinterface.h>
KDCStatusBarInterface *gKDCStatusBar;
#include <interfaces/kdcframegeneratorinterface.h>
KDCFrameGeneratorInterface *gKDCFrameGenerator;

K_PLUGIN_FACTORY( KDCDiscStructurePluginFactory, registerPlugin< KDCDiscStructurePlugin >(); )
K_EXPORT_PLUGIN( KDCDiscStructurePluginFactory( "KDCDiscStructurePlugin" ) )

KDCDiscStructurePlugin::KDCDiscStructurePlugin( QObject *parent, const QVariantList &args ) : KDCPluginInterface( parent ), pInfo( KPluginInfo("kdcdiscstructureplugin.desktop", "services") ), pInterfaces( NULL ), pStructureWidget( NULL ), pKDCGuiInterface( NULL )
{
	Q_UNUSED( args );
	gKDCJobsInterface = NULL;
	gKDCStatusBar = NULL;
	gKDCFrameGenerator = NULL;
}

KDCDiscStructurePlugin::~KDCDiscStructurePlugin()
{
	unload();
}

QString KDCDiscStructurePlugin::interfaceName() const
{
	return "KDCDiscStructurePlugin";
}

const KPluginInfo& KDCDiscStructurePlugin::info() const
{
	return pInfo;
}

QWidget* KDCDiscStructurePlugin::widget() const
{
	return pStructureWidget;
}

KDCConfigPageInterface* KDCDiscStructurePlugin::configPage() const
{
	return NULL;
}

KDC::Position KDCDiscStructurePlugin::tabPosition() const
{
	return KDC::Left;
}

bool KDCDiscStructurePlugin::load()
{
	if ( pStructureWidget )
		return false;
	
	pStructureWidget = new KDCDiscStructureWidget;
	connect( pStructureWidget, SIGNAL( currentObjectChanged( KDCObject * ) ), this, SIGNAL( objectChanged( KDCObject * ) ) );
	connect( pStructureWidget, SIGNAL( discChanged( KDCDisc * ) ), this, SLOT( slotDiscChanged( KDCDisc * ) ) );

	KActionMenu *addDiscAction = new KActionMenu( KIcon("media-optical"), i18n("New &Project.."), this );
	addDiscAction->setDelayed( FALSE );
	actionCollection()->addAction( "addDisc", addDiscAction );
	connect( addDiscAction->menu(), SIGNAL( triggered( QAction * ) ), pStructureWidget, SLOT( addDiscTriggered( QAction * ) ) );

	KAction *action = actionCollection()->addAction( "addDVDDisc" );
	action->setIcon( KIcon("media-optical-dvd") );
	action->setText( i18n("DVD Disc") );
	action->setData( KDC::DVD );
	addDiscAction->addAction( action );

	action = actionCollection()->addAction( "addCVDDisc" );
	action->setIcon( KIcon("media-optical") );
	action->setText( i18n("CVD Disc") );
	action->setData( KDC::CVD );
	addDiscAction->addAction( action );

	action = actionCollection()->addAction( "addSVCDDisc" );
	action->setIcon( KIcon("media-optical") );
	action->setText( i18n("SVCD Disc") );
	action->setData( KDC::SVCD );
	addDiscAction->addAction( action );

	action = actionCollection()->addAction( "addVCDDisc" );
	action->setIcon( KIcon("media-optical") );
	action->setText( i18n("VCD Disc") );
	action->setData( KDC::VCD );
	addDiscAction->addAction( action );
	
	action = actionCollection()->addAction( "addBDDisc" );
	action->setIcon( KIcon("media-optical-blu-ray") );
	action->setText( i18n("BD Disc") );
	action->setData( KDC::BLURAY );
	addDiscAction->addAction( action );
	pStructureWidget->toolBar()->addAction( action );
	
	action = actionCollection()->addAction( "addDIVXDisc" );
	action->setIcon( KIcon("media-optical") );
	action->setText( i18n("DivX Disc") );
	action->setData( KDC::DIVX );
	addDiscAction->addAction( action );
	
	action = actionCollection()->addAction( "addMenu", pStructureWidget, SLOT( addMenu() ) );
	action->setIcon( KIcon("insert-image") );
	action->setText( i18n("&Menu") );

	action = actionCollection()->addAction( "addVTS", pStructureWidget, SLOT( addVTS() ) );
	action->setIcon( KIcon("folder-yellow") );
	action->setText( i18n("&VTS") );
	
	action = actionCollection()->addAction( "addMovie", pStructureWidget, SLOT( addMovie() ) );
	action->setIcon( KIcon("application-x-mplayer2") );
	action->setText( i18n("M&ovie") );
	pStructureWidget->toolBar()->addAction( action );
	
	action = actionCollection()->addAction( "delete", pStructureWidget, SLOT( deleteObject() ) );
	action->setIcon( KIcon("edit-delete") );
	action->setText( i18n("Delete") );
	pStructureWidget->toolBar()->addAction( action );
	
	action = actionCollection()->addAction( "settings", pStructureWidget, SLOT( setDiscSettings() ) );
	action->setIcon( KIcon("configure") );
	action->setText( i18n("Disc settings") );
	pStructureWidget->toolBar()->addAction( action );

	action = actionCollection()->addAction( "splitmovie", pStructureWidget, SLOT( splitMovie() ) );
	action->setIcon( KIcon("format-break-node") );
	action->setText( i18n("Split movies") );
	pStructureWidget->toolBar()->addAction( action );
	
	action = actionCollection()->addAction( "joinmovie", pStructureWidget, SLOT( joinMovie() ) );
	action->setIcon( KIcon("format-join-node") );
	action->setText( i18n("Join movies") );
	pStructureWidget->toolBar()->addAction( action );
	
	action = actionCollection()->addAction( "ordermovie", pStructureWidget, SLOT( orderMovie() ) );
	action->setIcon( KIcon("format-list-ordered") );
	action->setText( i18n("Set order for movies") );
	pStructureWidget->toolBar()->addAction( action );
	
	setXMLFile("kdcdiscstructureplugin.rc");
	
	return true;
}

bool KDCDiscStructurePlugin::unload()
{
	if ( pKDCGuiInterface )
		pKDCGuiInterface->removeGuiPlugin( this );
	deletePtr( pStructureWidget );
	return true;
}

bool KDCDiscStructurePlugin::setInterfaces( QList<KDCBaseInterface*>& aInterfaces )
{
	pInterfaces = &aInterfaces;
	return true;
}

bool KDCDiscStructurePlugin::init()
{
	foreach ( KDCBaseInterface *interface, *pInterfaces )
		if ( interface->interfaceName() == "KDCJobsInterface" )
			gKDCJobsInterface = static_cast<KDCJobsInterface*>( interface );
		else
			if ( interface->interfaceName() == "KDCStatusBarInterface" )
				gKDCStatusBar = static_cast<KDCStatusBarInterface*>( interface );
		else
			if ( interface->interfaceName() == "KDCGuiInterface" )
				pKDCGuiInterface = static_cast<KDCGuiInterface*>( interface );
		else
			if ( interface->interfaceName() == "KDCFrameGeneratorInterface" )
				gKDCFrameGenerator = static_cast<KDCFrameGeneratorInterface*>( interface );
	if ( pKDCGuiInterface )
		pKDCGuiInterface->addGuiPlugin( this );
	return true;
}

void KDCDiscStructurePlugin::setObject( KDCObject *aObject )
{
	Q_UNUSED( aObject );
}

void KDCDiscStructurePlugin::refresh()
{
	
}

KDCObject* KDCDiscStructurePlugin::currentObject() const
{
	return NULL;
}

void KDCDiscStructurePlugin::slotDiscChanged( KDCDisc *aDisc )
{
	switch ( aDisc->type() )
	{
		case KDC::DVD: stateChanged("dvd"); break;
		case KDC::VCD:
		case KDC::SVCD:
		case KDC::CVD: stateChanged("xvcd"); break;
	}
	if ( aDisc && gKDCStatusBar )
	{
		gKDCStatusBar->setProjectSize( aDisc->size(), aDisc->settings()->mediaSize() );
		gKDCStatusBar->setProjectOutputPath( aDisc->settings()->outputPath() );
		gKDCStatusBar->setProjectTempPath( aDisc->settings()->tempPath() );
		gKDCStatusBar->setDeleteInputFilesEnabled( aDisc->settings()->deleteOryginalFiles() );
		gKDCStatusBar->setDeleteTempFilesEnabled( aDisc->settings()->deleteTempFiles() );
		gKDCStatusBar->setCreateDiscStructureEnabled( aDisc->settings()->createStructure() );
		gKDCStatusBar->refresh();
	}
}

#include "kdcdiscstructureplugin.moc"
